<?php

require_once '../config.php';
require_once '../functions.php';


books_get_current_user();

function add_users()
{
    if (empty($_POST['title'])) {
        $GLOBALS['message'] = "请填写标题";
        $GLOBALS['success'] = false;
        return;
    }
    if (empty($_POST['content'])) {
        $GLOBALS['message'] = "请填写内容";
        $GLOBALS['success'] = false;
        return;
    }
    if (empty($_POST['name'])) {
        $GLOBALS['message'] = "请填写用户名";
        $GLOBALS['success'] = false;
        return;
    }
    if (empty($_POST['number'])) {
        $GLOBALS['message'] = "请填写点赞数";
        $GLOBALS['success'] = false;
        return;
    }
  
    //接收并保存
    $pic = '/static/assets/img/default.png';
    $title = $_POST['title'];
    $content = $_POST['content'];
    $name = $_POST['name'];
    $number = $_POST['number'];
  
    $rows = book_execute("insert into sentence (id,content,title,number,picture,name)values (null,'{$content}','{$title}','{$number}','{$pic}','{$name}');");
    var_dump($rows);
    $GLOBALS['success'] = $rows > 0;
    $GLOBALS['message'] = $rows <= 0 ? '添加失败！' : '添加成功！';
}


//编辑功能
function edit_users()
{
    global $current_edit_users;
    $id = $current_edit_users['id'];
   
    
    $content = empty($_POST['content']) ? $current_edit_users['content'] : $_POST['content'];
    $current_edit_users['title'] = $content;
    $title = empty($_POST['title']) ? $current_edit_users['title'] : $_POST['title'];
    $current_edit_users['title'] = $title;
    $name = empty($_POST['name']) ? $current_edit_users['name'] : $_POST['name'];
    $current_edit_users['name'] = $name;
    $number = empty($_POST['number']) ? $current_edit_users['number'] : $_POST['number'];
    $current_edit_users['number'] = $number;

    $rows = book_execute("update sentence set content ='{$content}',title = '{$title}', name = '{$name}',number='{$number}' where id = '{$id}'");
    $GLOBALS['success'] = $rows > 0;
    $GLOBALS['success'] = $rows <= 0 ? '更新成功！':'更新失败';
}
  if (empty($_GET['id'])) {
      //  添加
      if ($_SERVER['REQUEST_METHOD']==='POST') {
          add_users();
      }
  } else {
      $current_edit_users =book_fetch_one('select * from sentence where id ='. $_GET['id']);
      if ($_SERVER['REQUEST_METHOD']==='POST') {
          edit_users();
      }
  }




// 查询全部数据分类
$lists = book_fetch_all('select * from sentence;');


?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Users &laquo; Admin</title>
  <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="/static/assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="/static/assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="/static/assets/css/admin.css">
  <script src="/static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
  <script>NProgress.start()</script>

  <div class="main">

  <?php include 'inc/navbar_admin.php'; ?>
    <div class="container-fluid">
      <div class="page-title">
        <h1>用户</h1>
      </div>
     <?php if (isset($success)): ?>
     <?php if ($success):?>
              <!-- 有错误信息时展示 -->
      <div class="alert alert-success">
        <strong>修改成功！</strong><?php echo $message;?>
      </div>
    <?php else:?>
       <div class="alert alert-danger">
        <strong>修改错误！</strong><?php echo $message;?>
      </div>
     <?php endif ?>
     <?php endif ?>
      <div class="row">
        <div class="col-md-4">
        <?php if (isset($current_edit_users)): ?>
           <form action="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php echo $current_edit_users['id']; ?>" method="post">
            <h2>编辑 <?php echo $current_edit_users['name'] ?> </h2>
            <div class="form-group">
              <label for="title">题目</label>
              <input id="title" class="form-control" name="title" type="title" placeholder="题目" value="<?php echo $current_edit_users['title'];?> " >
            </div>
            <div class="form-group">
              <label for="name">作者</label>
              <input id="name" class="form-control" name="name" type="text" placeholder="作者" value="<?php echo $current_edit_users['name'];?> ">
            </div>
            <div class="form-group">
              <label for="number">点赞</label>
              <input id="number" class="form-control" name="number" type="text" placeholder="点赞" value="<?php echo $current_edit_users['number'];?> ">
            </div>
            <div class="form-group">
              <label for="content">内容</label>
              <!-- <textarea class="form-group" name="content" id="content" cols="30" rows="10"></textarea> -->
              <textarea class="form-control text-index:2em" name="content" id="content" cols="30" rows="5"><?php echo $current_edit_users['content'];?></textarea>

            </div>
            <div class="form-group">
              <button class="btn btn-primary" type="submit">保存</button>
            </div>
          </form>
        <?php else: ?>
           <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <h2>添加数据</h2>
            <div class="form-group">
              <label for="title">题目</label>
              <input id="title" class="form-control" name="title" type="title" placeholder="题目" >
            </div>
            <div class="form-group">
              <label for="content">内容</label>
              <input id="content" class="form-control" name="content" type="text" placeholder="内容">
            </div>
            <div class="form-group">
              <label for="name">作者</label>
              <input id="name" class="form-control" name="name" type="text" placeholder="作者">
            </div>    
            <div class="form-group">
              <label for="number">点赞</label>
              <textarea class="form-control" name="number" id="number" cols="30" rows="5" placeholder="来啊，看过来"></textarea>
            </div>

            <div class="form-group">
              <button class="btn btn-primary" type="submit">添加</button>
            </div>
          </form>
        <?php endif ?>
         
        </div>
        <div class="col-md-8">
          <div class="page-action">
            <!-- show when multiple checked -->
            <a id="btn_delete" class="btn btn-danger btn-sm" href="/admin/users-delete.php" style="display: none">批量删除</a>
          </div>
          <table class="table table-striped table-bordered table-hover" >
            <thead>
               <tr>
                <th class="text-center" width="80">图片</th>
                <th>题目</th>
                <th style="width:400px">内容</th>
                <th>名字</th>
                <th>点赞</th>
                <th class="text-center" width="100">操作</th>
              </tr>
            </thead>
            <tbody>
            <?php foreach ($lists as $item): ?>
              <tr>

                <td class="text-center"><img class="avatar" src="<?php echo ($item['picture']) ? '/static/assets/img/default.png' : $item['avatar']; ?>"></td>
                <td><?php echo $item['title']; ?></td>
                <td><?php echo $item['content']; ?></td>
                <td><?php echo $item['name']; ?></td>
                <td><?php echo $item['number']; ?></td>
                <td class="text-center">
                  <a href="/admin/option.php?id=<?php echo $item['id']; ?>" class="btn btn-default btn-xs">编辑</a>
                  <a href="/admin/option-delete.php?id=<?php echo $item['id'] ?>" class="btn btn-danger btn-xs">删除</a>
                </td>
              </tr>
            <?php endforeach ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

  <?php $current_page = 'option'; ?>
  <?php include 'inc/sidebar.php'; ?>

  <script src="/static/assets/vendors/jquery/jquery.js"></script>
  <script src="/static/assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script>NProgress.done()</script>
</body>
</html>
